package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.PerformanceSalary;
import com.amsp.oa.oademo.service.PerformanceSalaryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/performance")
@Api(tags = "绩效工资审批表管理")
public class PerformanceSalaryController {

    @Autowired
    private PerformanceSalaryService performanceSalaryService;

    @PostMapping("/save")
    @ApiOperation("保存绩效工资审批表")
    public ResponseEntity<?> save(@RequestBody PerformanceSalary performanceSalary) {
        try {
            // 判断是新增还是更新
            boolean isUpdate = performanceSalary.getId() != null;
            if (isUpdate) {
                // 检查记录是否存在
                PerformanceSalary existingRecord = performanceSalaryService.getById(performanceSalary.getId());
                if (existingRecord == null) {
                    return ResponseEntity.badRequest().body("更新失败：记录不存在");
                }
                performanceSalaryService.updateById(performanceSalary);
                return ResponseEntity.ok().body("更新成功");
            } else {
                // 新增时设置id为null，让数据库自动生成
                performanceSalary.setId(null);
                performanceSalaryService.save(performanceSalary);
                return ResponseEntity.ok().body("保存成功");
            }
        } catch (Exception e) {
            log.error("保存绩效工资审批表失败", e);
            return ResponseEntity.badRequest().body("保存失败: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    @ApiOperation("获取绩效工资审批表列表")
    public ResponseEntity<?> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            Page<PerformanceSalary> page = new Page<>(current, size);
            Page<PerformanceSalary> result = performanceSalaryService.page(page);
            return ResponseEntity.ok().body(result);
        } catch (Exception e) {
            log.error("获取绩效工资审批表列表失败", e);
            return ResponseEntity.badRequest().body("获取列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/view/{id}")
    @ApiOperation("查看绩效工资审批表详情")
    public ResponseEntity<?> view(@PathVariable Long id) {
        try {
            PerformanceSalary performanceSalary = performanceSalaryService.getById(id);
            if (performanceSalary == null) {
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok().body(performanceSalary);
        } catch (Exception e) {
            log.error("查看绩效工资审批表详情失败", e);
            return ResponseEntity.badRequest().body("获取详情失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除绩效工资审批表")
    public ResponseEntity<?> delete(@PathVariable Long id) {
        try {
            boolean success = performanceSalaryService.removeById(id);
            if (success) {
                return ResponseEntity.ok().body("删除成功");
            } else {
                return ResponseEntity.badRequest().body("删除失败：记录不存在");
            }
        } catch (Exception e) {
            log.error("删除绩效工资审批表失败", e);
            return ResponseEntity.badRequest().body("删除失败: " + e.getMessage());
        }
    }
}